// write all jquery here
$(document).ready(function(){
    
    if(!$('body').hasClass('ajax'))
        $('a').tooltip({
            delay: 0,
            showURL: false,
            fade: 250
        });
    
    
    if($.cookie('dd_admin')=='fixed'){
        
        $('.switch_layout').html('Liquid Layout')
        if(!$('body').hasClass('ajax'))
            $('body').addClass('fixed')    
    }
    

    
    // flashing the msg
    if($('.flash_box p').text()!=''){
        $('.flash_box').fadeIn(500)
    }
            
    $('.menu-toggle').click(function(e){
        $(this).siblings('ul').slideToggle(200)
    })
    
    
    
    
    $('.sidenav .selected').each(function(){
        
        $(this).next('.subnav').slideDown()
    })
    
    $('tr:even').addClass('alt')
    
    
    
    
            
            
    
    $('.switch_layout').click(function(e){
        e.preventDefault()
        if($('body').hasClass('fixed')){
            $(this).html('Fixed Layout')
            $('body').removeClass('fixed')            
            $.cookie('dd_admin','liquid',{ expires: 1, path: adminpath})
            //$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
            
        }
        else {
            $(this).html('Liquid Layout')
            $('body').addClass('fixed')
            $.cookie('dd_admin','fixed',{ expires: 1, path: adminpath})
            
        }
        
    })
    
    if($('.sortable').length)
        $(function() {
            $(".sortable").sortable({
                connectWith: '.connectedSortable',
                revert: true
            }).disableSelection();
        });
    
    if($('.sortablegrid').length){
        $(function() {
            $(".sortablegrid").sortable({
                helper: function(e, tr)
                      {
                        var $originals = tr.children();
                        var $helper = tr.clone();
                        $helper.children().each(function(index)
                        {
                          // Set helper cell sizes to match the original sizes
                          $(this).width($originals.eq(index).width())
                        });
                        return $helper;
                      },
                revert: true,
                update: function(event, ui) {
                    $('.newsOrder').fadeIn(100)
                }

            }).disableSelection();
        });
        
        $('.newsOrder').click(function(e){
            e.preventDefault()
            var postdata='';
            $('.sortablegrid .row_id').each(function(){
                postdata+=$(this).val()+',';
            })
            
            $('#loading').removeClass('hide')
            
            $.post($('#ajax_path').val(),{"data[Ajax]":postdata},function(data){
                $('#loading').addClass('hide')
                
                
                if(data=='success')
                    window.location.reload(false) 
                else {
                    $.fancybox({
                        content:'Something is wrong, Please Try Again'
                    });
                    
                }
            })
        })
    
    }
        
    
})



// for blocks management page

$(document).ready(function(){
    function get_blocks(){
        var data='';
        $('.block').each(function(){
            data+=$(this).attr('rel')+'-';
            $(this).find('li').each(function(){
                data+=$(this).attr('rel')+'.'+$(this).index()+',';
            })
            data+='|';
        })
        data+=':';
        $('.inactive_block li').each(function(){
            data+=$(this).attr('rel')+'.'+$(this).index()+',';
        })
        
        return data;                            
    }
    
    $('.save_block').click(function(e){
        e.preventDefault()
        $('#loading').removeClass('hide');
        
        var b=get_blocks();
        
        
        
        $.post($('#ajax_path').val(),{"data[Ajax]":b},function(data){
            $('#loading').addClass('hide')
            
            
            if(data=='success')
                window.location.reload(false) 
            else {
                $.fancybox({
                    content:'Something is wrong, Please Try Again'
                });
                
                
            }
        })
        
    })
    
    
    
})






// for post

$(document).ready(function(){
    
    
    $('#postDate').datepicker({
        "dateFormat":'yy-mm-dd'

    }).change(function(e){
        window.location=adminpath+'admin/posts/index/'+$(this).val()   
    })
    
    
    
     $('.datepicker').datepicker({
        "dateFormat":'yy-mm-dd'

    })
     
    
    $('.selectCategory').change(function(e){
        e.preventDefault()
        
        
        window.location=adminpath+'admin/posts/index/'+$('#postDate').val()+'/'+$(this).val()
        
    })
    
    
    $('a.fancybox').fancybox({
        'width' :800,
        'height' :600,
        'titleShow'     : false,
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic',
        'type'          : 'iframe'
    });
    
    $('a[rel=mediaimage]').fancybox({
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic',
    });
    
    
    
    
    if($(".autocomplete").length){
        $(".autocomplete").autocomplete({
            source: $('#ajax_path').val()
    });    
  }
    
    
    
})




Shadowbox.init();        